import type { Recordable } from '@vben/types';

import { realApiClient } from '#/api/request';

export namespace VisitorApi {
  /** 游人列表查询参数 */
  export interface TalentListParams {
    pageNo: number;
    pageSize: number;
    keyword?: string;
    profession?: string[];
    expertiseField?: string[];
    skillTags?: string[];
    experienceYearsStart?: number;
    experienceYearsEnd?: number;
    sex?: string;
    status?: string;
    isFavorite?: 0 | 1;
    queryAll?: 0 | 1;
  }

  /** 游人记录 */
  export interface TalentRecord {
    id: string;
    realName: string;
    status: string;
    status_dictText?: string;
    statusType?: string;
    sex: string;
    sex_dictText?: string;
    age?: number;
    phone: string;
    email: string;
    profession: string;
    profession_dictText?: string;
    city: string;
    city_dictText?: string;
    expertiseField: string;
    expertiseField_dictText?: string;
    skillTags: string;
    skillTags_dictText?: string;
    experienceYears: number;
    avator: string;
    enterprise: string;
    enterprise_dictText?: string;
    summary: string;
    personalDetails: string;
    attachs: string;
    createTime: string;
    updateTime: string;
    createBy: string;
    updateBy: string;
    sysOrgCode: string;
    collected: number;
    rating?: Recordable<any>;
  }

  /** 游人列表返回结果 */
  export interface TalentListResult {
    current: number;
    size: number;
    records: TalentRecord[];
    total: number;
    pages: number;
  }

  /** 收藏/取消收藏参数 */
  export interface FavoriteParams {
    talentId: string;
    isFavorite: 0 | 1;
  }

  export interface DictItem {
    value: string;
    text: string;
    color?: null | string;
    jsonObject?: null | Recordable<any>;
    title?: string;
    label?: string;
  }

  export interface TalentDictResult {
    sex: DictItem[];
    gtms_frozen_operation: DictItem[];
    gtms_expertise_field: DictItem[];
    gtms_frozen_status: DictItem[];
    gtms_account_review_status: DictItem[];
    gtms_job_post: DictItem[];
    gtms_talent_status: DictItem[];
    gtms_skill_tag: DictItem[];
  }

  /** 企业人才统计 */
  export interface TalentCountResult {
    data: any;
    total: number;
    external: number;
    testing: number;
    finished: number;
    favorite: number;
  }

  export interface TalentDetail extends TalentRecord {
    personalDetails?: string;
    summary?: string;
    statusText?: string;
    sexText?: string;
    cityText?: string;
    expertiseFieldText?: string;
    skillTagsText?: string;
    professionText?: string;
    attachs?: string;
    birthDate?: string;
  }
}

/**
 * 查询游人列表
 */
export async function getTalentListApi(params: VisitorApi.TalentListParams) {
  const response = await realApiClient.post<any>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/talent/list',
    params,
  );

  // debugger;
  const result: VisitorApi.TalentListResult = response?.data
    ? response?.data
    : response;

  return result;
}

/**
 * 添加或取消收藏游人
 */
export async function updateTalentFavoriteApi(
  params: VisitorApi.FavoriteParams,
) {
  const response = await realApiClient.post<any>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/favorite',
    params,
  );

  return response?.result ?? response?.data?.result ?? response;
}

/**
 * 获取人才统计信息
 */
export async function getTalentCountApi() {
  const response = await realApiClient.get<any>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/talent/count',
    {},
  );

  const result: VisitorApi.TalentCountResult =
    response?.result ?? response?.data?.result ?? response;

  return result;
}

export async function getTalentDictApi() {
  const response = await realApiClient.get<any>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/dict/all',
  );

  const payload = response?.data ?? response;
  const result = (payload?.result ?? payload) as VisitorApi.TalentDictResult;

  return result;
}

/**
 * 获取游人详情
 */
export async function getTalentDetailApi(userId: string) {
  const response = await realApiClient.get<any>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/talent/detail',
    {
      params: { userId },
    },
  );

  const payload = response?.data ?? response;
  const result = (payload?.result ?? payload) as VisitorApi.TalentDetail;

  return result;
}
